package com.fstech.whiteboadrd.cahrt;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.fstech.whiteboadrd.R;
import com.fstech.whiteboadrd.fragment.WhiteBoardFragment;
import com.fstech.whiteboadrd.utils.UIUtils;
import com.fstech.whiteboadrd.widget.WheelView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ChartGuideDialog extends Dialog implements View.OnClickListener {

    private ImageView mChartguideClose;
    private EditText mChartguideNameinput;
    private WheelView mChartguideWheel;
    private RadioButton mGuideChart1;
    private RadioButton mGuideChart2;
    private RadioButton mGuideChart3;
    private TextView mGuideNext;
    private RadioGroup guide_check;
    private View itemView;

    ChartCallback mCallback;

    Map<EditText, EditText> editTextMap = new LinkedHashMap<>();
    Map<String, Double> mDataSet = new LinkedHashMap<>();
    private String chartName;

    private int chartMode = 1;

    public ChartGuideDialog(@NonNull Context context) {
        super(context);
        init();
    }

    public ChartGuideDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    protected ChartGuideDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init();
    }

    private void setGuide1(int count) {
        setContentView(R.layout.layout_dialog_chartguide1);
        LinearLayout guide1_items = findViewById(R.id.guide1_items);
        ScrollView guide1_container = findViewById(R.id.guide1_container);
        TextView guide1_next = findViewById(R.id.guide1_next);
        editTextMap.clear();
        for (int i = 0; i < count; i++) {
            guide1_items.addView(getItemView(i));
        }
        guide1_next.setOnClickListener(this);
        findViewById(R.id.chartguide1_close).setOnClickListener(this);
        findViewById(R.id.chartguide1_bac).setOnClickListener(this);
    }

    public View getItemView(int index) {
        View itemView = View.inflate(getContext(), R.layout.item_data, null);
        TextView item_index = itemView.findViewById(R.id.item_index);
        item_index.setText(index + 1 + "");
        EditText item_key = itemView.findViewById(R.id.item_key);
        EditText item_value = itemView.findViewById(R.id.item_value);
        editTextMap.put(item_key, item_value);
        return itemView;
    }

    private void init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_dialog_chartguide);
        setCanceledOnTouchOutside(false);
        setCancelable(true);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        initView();
        initEvent();
    }

    private void initEvent() {
        List<String> items = new ArrayList<>();
        for (int i = 1; i < 20; i++) {
            items.add(i + "");
        }
        mChartguideWheel.setItems(items);
        mChartguideWheel.setSeletion(6);
        guide_check.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.guide_chart1:
                        chartMode = 1;
                        break;
                    case R.id.guide_chart2:
                        chartMode = 2;
                        break;
                    case R.id.guide_chart3:
                        chartMode = 3;
                        break;
                }
            }
        });
        mGuideNext.setOnClickListener(this);
        mChartguideNameinput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mChartguideNameinput.setFocusable(true);
                mChartguideNameinput.setFocusableInTouchMode(true);
                mChartguideNameinput.requestFocus();
                KeyboardUtils.showSoftInput(mChartguideNameinput);
            }
        });
        mChartguideClose.setOnClickListener(this);
    }

    private void initView() {
        mChartguideClose = (ImageView) findViewById(R.id.chartguide_close);
        mChartguideNameinput = (EditText) findViewById(R.id.chartguide_nameinput);
        mChartguideWheel = (WheelView) findViewById(R.id.chartguide_wheel);
        mGuideChart1 = (RadioButton) findViewById(R.id.guide_chart1);
        mGuideChart2 = (RadioButton) findViewById(R.id.guide_chart2);
        mGuideChart3 = (RadioButton) findViewById(R.id.guide_chart3);
        mGuideNext = (TextView) findViewById(R.id.guide_next);
        guide_check = findViewById(R.id.guide_check);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.chartguide_close:
            case R.id.chartguide1_close:
                dismiss();
                break;
            case R.id.guide1_next:
                checkData();
                break;
            case R.id.guide_next:
                chartName = mChartguideNameinput.getText().toString();
//                if (TextUtils.isEmpty(chartName)) {
//                    UIUtils.showToastSafe(R.string.chartguide_tip5);
//                    return;
//                }
                setGuide1(mChartguideWheel.getSeletedIndex() + 1);
                break;
        }
    }

    private void checkData() {
        Set<EditText> editTexts = editTextMap.keySet();
        mDataSet.clear();
        for (EditText editText : editTexts) {
            String key = editText.getText().toString();
            if (TextUtils.isEmpty(key)) {
                UIUtils.showToastSafe(R.string.chartguide1_tip1);
                return;
            }
            EditText editTextValue = editTextMap.get(editText);
            String value = editTextValue.getText().toString();
            try {
                Double aDouble = Double.valueOf(value);
                mDataSet.put(key, aDouble);
            } catch (Exception e) {
                UIUtils.showToastSafe(UIUtils.getString(R.string.chartguide1_tip3) + "\"" + value + "\"" + UIUtils.getString(R.string.chartguide1_tip2));
                return;
            }
            if (TextUtils.isEmpty(value)) {
                UIUtils.showToastSafe(R.string.chartguide1_tip1);
                return;
            }
        }
        Bitmap bitmap = generateChart();
        if (mCallback != null) {
            mCallback.onChartGenerated(bitmap);
        }
        dismiss();
    }

    private Bitmap generateChart() {
//        View chartView = View.inflate(getContext(), R.layout.layout_chart, null);
//        RelativeLayout rl_chart = chartView.findViewById(R.id.rl_chart);
//        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
//                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//        //居中显示
//        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
//        rl_chart.addView(ChartProvider.getBarChartView(getContext(), mDataSet, chartName), layoutParams);

        View barChartView = null;

        switch (chartMode) {
            case 1:
                barChartView = ChartProvider.getBarChartView(getContext(), mDataSet, chartName);
                break;
            case 2:
                barChartView = ChartProvider.getLineChartView(getContext(), mDataSet, chartName);
                break;
            case 3:
                barChartView = ChartProvider.getPieChartView(getContext(), mDataSet, chartName);
                break;

        }
        return ChartProvider.loadBitmapFromView(barChartView);
    }

    public void setCallback(ChartCallback callback) {
        mCallback = callback;
    }

    public interface ChartCallback {
        void onChartGenerated(Bitmap bitmap);
    }

}
